/***************************************************************************
 创建者: hualei
 开始时间: 2020-09-29
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2020.9.29 类的具体实现代码编写

 ***************************************************************************
 *  TCP以太网通信实现类                                                                       *
 ***************************************************************************/
#include "tcpmanager.h"
#include "soapStub.h"
#include "messagelog.h"
#include "qabstractsocket.h"

TcpManager::TcpManager()
{
    //导致在调用者的线程创建了socket对象，那么接收也必须在等待该线程有空了，才收的到。
    qDebug() << "++++++++++++Client Start connect flag: "
             << QObject::connect(this, SIGNAL(tcpStart_client_signal(QString,int)),
                                       this, SLOT(tcpStart_client_slot(QString,int)),Qt::DirectConnection);
    qDebug() << "++++++++++++Server Start connect flag: "
             << QObject::connect(this, SIGNAL(tcpStart_Server_signal(QString,int)),
                                        this, SLOT(tcpStart_server_slot(QString,int)),Qt::DirectConnection);
    qDebug() << "+++++++++++++Client Read connect flag: "
             << QObject::connect(this, SIGNAL(tcpRead_client_signal(QString,int,QByteArray&)),
                                       this, SLOT(tcpRead_Client_slot(QString,int,QByteArray&)),Qt::DirectConnection);
    qDebug() << "+++++++++++++Server Read connect flag: "
             << QObject::connect(this, SIGNAL(tcpRead_server_signal(QString,int,QByteArray&)),
                                        this, SLOT(tcpRead_Server_slot(QString,int,QByteArray&)),Qt::DirectConnection);
    qDebug() << "++++++++++++Client Write connect flag: "
             << QObject::connect(this, SIGNAL(tcpWrite_clien_signal(QString,int,QByteArray)),
                                         this, SLOT(tcpWrite_Client_slot(QString,int,QByteArray)),Qt::DirectConnection);
    qDebug() << "++++++++++++Server Write connect flag: "
             << QObject::connect(this, SIGNAL(tcpWrite_server_signal(QString,int,QByteArray)),
                                         this, SLOT(tcpWrite_Server_slot(QString,int,QByteArray)),Qt::DirectConnection);
    qDebug() << "++++++++++++++++SendData connect flag: "
             << QObject::connect(this, SIGNAL(sendData_signal(QTcpSocket,QByteArray,bool&)),
                                         this, SLOT(sendData_slot(QTcpSocket,QByteArray,bool&)),Qt::DirectConnection);

    readFinishedStatus = false;
    startFinishedStatus = false;
    writeFinishedStatus = false;
}

int TcpManager::tcpStart_client(const QString &ip, int port)
{  
    emit tcpStart_client_signal(ip,port);
    if(!startFinishedStatus)
    {
        return -1;
    }
    return 1;
}

int TcpManager::tcpStart_server(const QString &ip, int port)
{
    emit tcpStart_Server_signal(ip, port);
    if(!startFinishedStatus)
    {
        return -1;
    }
    return 1;
}

int TcpManager::tcpWrite_client(const QString &ip, int port, const QByteArray &dataIn)
{
    emit tcpWrite_clien_signal(ip,port,dataIn);
    if(!writeFinishedStatus)
    {
        return -1;
    }
    return 1;
}

int TcpManager::tcpWrite_server(const QString &ip, int port, const QByteArray &dataIn)
{
    emit tcpWrite_server_signal(ip,port,dataIn);
    if(!writeFinishedStatus)
    {
        return -1;
    }

    return 1;
}


int TcpManager::tcpRead_client(const QString& ip, int port,QByteArray& dataOut)
{
    emit tcpRead_client_signal(ip,port,dataOut);
    if(!readFinishedStatus)
    {
        return -1;
    }

    return 1;
}

int TcpManager::tcpRead_server(const QString &ip, int port, QByteArray &dataOut)
{
    emit tcpRead_server_signal(ip,port,dataOut);

    if(!readFinishedStatus)
    {
        return -1;
    }

    return 1;
}

int TcpManager::tcpStop_client(const QString &ip, int port)
{
    QString tmpId=ip+":"+QString::number(port);
    // 检查是否已存在响应的Socket对象
    if(tcpClientList.keys().indexOf(tmpId) == -1)
    {
        qDebug() << "断开客户端连接失败:无此IP对应的Socket对象!";
        return -1;
    }

    // 与主机断开连接
    tcpClientList.value(tmpId)->disconnectFromHost();
    // 删除对应的Socket对象
    mutex.lock();
    tcpClientList.remove(tmpId);
    mutex.unlock();

    return 1;
}

int TcpManager::tcpStop_server(const QString &ip, int port)
{
    Q_UNUSED(ip);
    if(tcpServerList.keys().indexOf(port) == -1)
    {
        qDebug() << "断开服务器端连接失败:无此IP对应的Server对象!";
        return -1;
    }

    tcpServerList.value(port)->close();
    mutex.lock();
    tcpServerList.remove(port);
    mutex.unlock();

    return 1;
}

int TcpManager::setTcpRobotId(const int &robotIdin)
{
    return robotId = robotIdin;
}

void TcpManager::incomingConnection(qintptr handle)
{
    //超出最大连接数量关闭连接
    if (tcpClientList.size() > this->maxPendingConnections()) {
        QTcpSocket tcp;
        tcp.setSocketDescriptor(handle);
        tcp.disconnectFromHost();
        return;
    }
    // 创建初始化新连接的Socket对象
    QTcpSocket *clientSocket = new QTcpSocket;
    connect(clientSocket,SIGNAL(error(QAbstractSocket::SocketError)),\
                this,SLOT(socketError(QAbstractSocket::SocketError)),Qt::DirectConnection);
    if(!clientSocket->setSocketDescriptor(handle))
    {
        qDebug() << "tcpManager 建立客户端连接失败:" << clientSocket->errorString();
        delete clientSocket;
        return;
    }

    mutex.lock();
    tcpClientList.insert(clientSocket->peerAddress().toString(),clientSocket);
    mutex.unlock();
}

void TcpManager::tcpStart_client_slot(const QString &ip, int port)
{
    QString tmpId=ip+":"+QString::number(port);
    if(tcpClientList.keys().indexOf(tmpId) == -1)
    {
        QTcpSocket *tmpClientSocket = new QTcpSocket;
        qDebug() << " +++++++++connected flag:"
                 << connect(tmpClientSocket,SIGNAL(error(QAbstractSocket::SocketError)),
                            this,SLOT(socketError_slot(QAbstractSocket::SocketError)),Qt::DirectConnection);
        tmpClientSocket->abort();
        tmpClientSocket->connectToHost(ip,port);
        if(!tmpClientSocket->waitForConnected(500))//会堵塞等待在这里设定的时间。其它主线程驱动的逻辑会受影响。
        {
            // 登录失败
            qDebug() << "TcpManager::tcpStart_client_slot,TCP_START 登录失败:" << tmpClientSocket->errorString();
            delete tmpClientSocket;
            startFinishedStatus = false;
            return;
        }
        startFinishedStatus = true;
        mutex.lock();
        tcpClientList.insert(tmpId,tmpClientSocket);
        mutex.unlock();
    }
    else
    {
//        if(tcpClientList.value(ip) == NULL)
//        {
//            QTcpSocket *tmpClientSocket = new QTcpSocket;
//            // 关联socket error信号，处理客户端对象在通信过程中的各种异常
//            connect(tmpClientSocket,SIGNAL(error(QAbstractSocket::SocketError)),\
//                        this,SLOT(socketError_slot(QAbstractSocket::SocketError)),Qt::DirectConnection);

//            tmpClientSocket->abort();
//            tmpClientSocket->connectToHost(ip,port);
//            if(!tmpClientSocket->waitForConnected(3000))
//            {
//                // 登录失败
//                qDebug() << "TCP_START 登录失败:" << tmpClientSocket->errorString();
//                startFinishedStatus = false;
//                return;
//            }
//            startFinishedStatus = true;
//            mutex.lock();
//            tcpClientList[ip] = tmpClientSocket;
//            mutex.unlock();
//        }
//        else
//        {
//            connect(tcpClientList.value(ip),SIGNAL(error(QAbstractSocket::SocketError)),\
//                        this,SLOT(socketError_slot(QAbstractSocket::SocketError)),Qt::DirectConnection);

//            tcpClientList.value(ip)->abort();
//            tcpClientList.value(ip)->connectToHost(ip,port);
//            if(!tcpClientList.value(ip)->waitForConnected(3000))
//            {
//                // 登录失败
//                qDebug() << "TCP_START 登录失败:" << tcpClientList.value(ip)->errorString();
//                startFinishedStatus = false;
//                return;
//            }
//        }
        // 删除之前链接,创建新链接
//        delete tcpClientList.value(ip);
//        tcpClientList.remove(ip);
//        QTcpSocket *tmpClientSocket = new QTcpSocket;
//        qDebug() << " +++++++++connected flag:"
//                 << connect(tmpClientSocket,SIGNAL(error(QAbstractSocket::SocketError)),
//                            this,SLOT(socketError_slot(QAbstractSocket::SocketError)),Qt::DirectConnection);
//        tmpClientSocket->abort();
//        tmpClientSocket->connectToHost(ip,port);
//        if(!tmpClientSocket->waitForConnected(30000))
//        {
//            // 登录失败
//            qDebug() << "TCP_START 登录失败:" << tmpClientSocket->errorString();
//            startFinishedStatus = false;
//            return;
//        }
//        startFinishedStatus = true;
//        mutex.lock();
//        tcpClientList.insert(ip,tmpClientSocket);
//        mutex.unlock();
        if(tcpClientList.value(tmpId)->state() == QTcpSocket::ConnectedState)
        {
            // 已连接
            startFinishedStatus = true;
            return;
        }
        else
        {
            // 未连接
            tcpClientList.value(tmpId)->abort();
            tcpClientList.value(tmpId)->connectToHost(ip,port);
            if(!tcpClientList.value(tmpId)->waitForConnected(800))
            {
                // 登录失败
                qDebug() << "TCP_START 登录失败:" << tcpClientList.value(tmpId)->errorString();
                startFinishedStatus = false;
                delete tcpClientList.value(tmpId);
                tcpClientList.remove(tmpId);
                return;
            }
        }
    }

    startFinishedStatus = true;
}

void TcpManager::tcpStart_server_slot(const QString &ip, int port)
{
    Q_UNUSED(ip);
    if(tcpServerList.keys().indexOf(port) == -1)
    {
        QTcpServer *tmpServer = new QTcpServer;
        if(!tmpServer->listen(QHostAddress::Any,port))
        {
            qDebug() << "TcpManager::tcpStart_server_slot, TcpManager Server Listen Port Error:"
                     << tmpServer->errorString();
            startFinishedStatus = false;
            delete tmpServer;
            return;
        }
        connect(tmpServer, SIGNAL(newConnection()),  this, SLOT(newConnectionSlot()));
        templateServer = tmpServer;
        startFinishedStatus = true;
        mutex.lock();
        tcpServerList.insert(port,tmpServer);
        mutex.unlock();
    }
    else
    {
//        if(tcpServerList.value(port) == NULL)
//        {
//            QTcpServer *tmpServer = new QTcpServer;
//            if(!tmpServer->listen(QHostAddress::Any,port))
//            {
//                qDebug() << "TcpManager Server Listen Port Error:" << tmpServer->errorString();
//                startFinishedStatus = false;
//                return;
//            }
//            startFinishedStatus = true;
//            mutex.lock();
//            tcpServerList.insert(port,tmpServer);
//            mutex.unlock();
//            tcpServerList.value(port)

//        }
//        else
//        {
            if(!tcpServerList.value(port)->isListening())
            {
                if(!tcpServerList.value(port)->listen(QHostAddress::Any, port))
                {
                    qDebug() << "TcpManager Server Listen Port Error:" << tcpServerList.value(port)->errorString();
                    startFinishedStatus = false;
                    delete tcpServerList.value(port);
                    tcpServerList.remove(port);
                    return;
                }
            }
//        }
        // 虚函数incomingConnection,当有客户端连接时触发
//    }
    }
    startFinishedStatus = true;
}

void TcpManager::tcpRead_Client_slot(const QString &ip, int port, QByteArray &dataOut)
{
    QString tmpId=ip+":"+QString::number(port);
    if(tcpClientList.keys().indexOf(tmpId) == -1)
    {
        qDebug() << "tcpRead Client Error:无客户端Socket对象!";
        readFinishedStatus = false;
        return;
    }
    else
    {
        if(!tcpClientList.value(tmpId)->isValid())
        {
            readFinishedStatus = false;
            return;
        }

//        // 阻塞接收
//        while (tcpClientList.value(ip)->bytesAvailable() < (int)nSize) {
//                     if (!tcpClientList.value(ip)->waitForReadyRead(3000)) {
//                         emit error(tcpClientList.value(ip)->error(),tcpClientList.value(ip)->errorString());
//                         return;
//                     }
//                 }
//        // 阻塞接收数据
//        while (!tcpClientList.value(ip)->waitForReadyRead()) {
//            readFinishedStatus = false;
//            qDebug() << "TCP_READ Timeout :" << tcpClientList.value(ip)->errorString();
//        }
//        if(!tcpClientList.value(ip)->waitForReadyRead(100))//hualei不要堵塞等待
//        {
//            readFinishedStatus = false;
//            return;
//        }
        dataOut = tcpClientList.value(tmpId)->readAll();
        tcpClientList.value(tmpId)->flush();
//        tcpClientList.value(ip)->disconnectFromHost();
//            if(dataOut.isEmpty())
//            {
//                readFinishedStatus = false;
//                return;
//            }
        readFinishedStatus = true;
        qDebug() << "TCP_READ Recive Data:" << dataOut;
        return;
    }
}

void TcpManager::tcpRead_Server_slot(const QString &ip, int port, QByteArray &dataOut)
{
    // 检查端口是否被占用
    if(tcpServerList.keys().indexOf(port) == -1)
    {
        readFinishedStatus = false;
        qDebug() << "服务器接收失败:没有这个服务器对象!";
        return;
    }
    else
    {
        // 检查端口是否被监听中
        if(!tcpServerList.value(port)->isListening())
        {
            addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12037, robotId);
            readFinishedStatus = false;
            return;
            // 监听端口
//            if(!tcpServerList.value(port)->listen(QHostAddress::Any,port))
//            {
//                qDebug() << "tcpManager Server 监听" << port <<"端口失败:" << tcpServerList.value(port)->errorString();
//                readFinishedStatus = false;
//                return;
//            }
        }
        // 连接对象IP是否存在
        if(!serverPortMap.keys().contains(port))
        {
            readFinishedStatus = false;
            addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine",  12041, robotId);
            return;
        }

        QString tmpClientIp = serverPortMap.value(port);
        if(tcpClientList.value(tmpClientIp)->state() != QTcpSocket::ConnectedState)
        {
            addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12040, robotId);
            readFinishedStatus = false;
            return;
        }

//        if(!tcpClientList.value(tmpClientIp)->waitForReadyRead(100))//hualei不要堵塞等待
//        {
//            addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12036, robotId);
//            readFinishedStatus = false;
//            return;
//        }

        dataOut = tcpClientList.value(tmpClientIp)->readAll();
        tcpClientList.value(tmpClientIp)->flush();
        //为空不做异常处理
        if(dataOut.isEmpty())
        {
//            readFinishedStatus = false;
            qDebug() << "TcpManager Server Read :接收数据为空!";
//            return;
        }
    }
    readFinishedStatus = true;
}

void TcpManager::tcpWrite_Client_slot(const QString &ip, int port, const QByteArray &dataIn)
{
    QString tmpId=ip+":"+QString::number(port);
    if(tcpClientList.keys().indexOf(tmpId) == -1)
    {
        writeFinishedStatus = false;
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12033, robotId);
        return;
    }
    else
    {
        if(tcpClientList.value(tmpId)->state() != QTcpSocket::ConnectedState)
        {
            qDebug() << "TCP_WRITE socket 未连接:" << tcpClientList.value(tmpId)->errorString();
            addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12039, robotId);
            writeFinishedStatus = false;
            return;
        }

        qDebug() << "tmpManager client write Data:" << dataIn;
        tcpClientList.value(tmpId)->write(dataIn);
        if(!tcpClientList.value(tmpId)->waitForBytesWritten(200))
        {
            qDebug() << "TCP_WRITE 发送数据失败:" << tcpClientList.value(tmpId)->errorString();
            addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12035, robotId);
            writeFinishedStatus = false;
            return;
        }
        // 刷新缓存
        tcpClientList.value(tmpId)->flush();
//        tcpClientList.value(ip)->close();
    }
    writeFinishedStatus = true;
}

void TcpManager::tcpWrite_Server_slot(const QString &ip, int port, const QByteArray &dataIn)
{
    if(tcpServerList.keys().indexOf(port) == -1)
    {
        writeFinishedStatus = false;
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine",  12034, robotId);
        return;
    }
    else
    {
        if(!tcpServerList.value(port)->isListening())
        {
            writeFinishedStatus = false;
            addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine",  12034, robotId);
            return;
        }

        // 连接对象是否存在
        if(!serverPortMap.keys().contains(port))
        {
           writeFinishedStatus = false;
           addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine",  12042, robotId);
           return;
        }

        QString tmpClientIp = serverPortMap.value(port);
        if(tcpClientList.value(tmpClientIp)->state() != QTcpSocket::ConnectedState)
        {
            addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine",  12038, robotId);
            writeFinishedStatus = false;
            return;
        }

        // 发送数据
        tcpClientList.value(tmpClientIp)->write(dataIn);
        if(!tcpClientList.value(tmpClientIp)->waitForBytesWritten(100))
        {
            qDebug() << "TCP_WRITE 发送数据失败:" << tcpClientList.value(ip)->errorString();
            writeFinishedStatus = false;
            return;
        }
        tcpClientList.value(tmpClientIp)->flush();
    }
    writeFinishedStatus = true;
}

void TcpManager::sendData_slot(QTcpSocket *socketIn, const QByteArray &dataIn, bool &sendStatus)
{
    if(dataIn.isEmpty())
    {
        sendStatus = false;
        return;
    }
    if(!socketIn->write(dataIn))
    {
        qDebug() << "数据发送失败:" << socketIn->errorString();
        sendStatus = false;
        return;
    }
    socketIn->flush();
    sendStatus = true;
}

void TcpManager::socketError_slot(QAbstractSocket::SocketError errorNum)
{
    switch (errorNum)
    {
    case QAbstractSocket::ConnectionRefusedError:
    {
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12000, robotId);
        break;
    }
    case QAbstractSocket::RemoteHostClosedError:
    {
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12001, robotId);
        break;
    }
    case QAbstractSocket::HostNotFoundError:
    {
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12002, robotId);
        break;
    }
    case QAbstractSocket::SocketAccessError:
    {
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12003, robotId);
        break;
    }
    case QAbstractSocket::SocketResourceError:
    {
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12004, robotId);
        break;
    }
    case QAbstractSocket::SocketTimeoutError:
    {
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12005, robotId);
        break;
    }
    case QAbstractSocket::DatagramTooLargeError:
    {
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12006, robotId);
        break;
    }
    case QAbstractSocket::NetworkError:
    {
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12007, robotId);
        break;
    }
    case QAbstractSocket::AddressInUseError:
    {
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12008, robotId);
        break;
    }
    case QAbstractSocket::SocketAddressNotAvailableError:
    {
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12009, robotId);
        break;
    }
    case QAbstractSocket::UnsupportedSocketOperationError:
    {
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12010, robotId);
        break;
    }
    case QAbstractSocket::UnfinishedSocketOperationError:
    {
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12011, robotId);
        break;
    }
    case QAbstractSocket::ProxyAuthenticationRequiredError:
    {
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12012, robotId);
        break;
    }
    case QAbstractSocket::SslHandshakeFailedError:
    {
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12013, robotId);
        break;
    }
    case QAbstractSocket::ProxyConnectionRefusedError:
    {
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12014, robotId);
        break;
    }
    case QAbstractSocket::ProxyConnectionClosedError:
    {
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12015, robotId);
        break;
    }
    case QAbstractSocket::ProxyConnectionTimeoutError:
    {
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12016, robotId);
        break;
    }
    case QAbstractSocket::ProxyNotFoundError:
    {
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12017, robotId);
        break;
    }
    case QAbstractSocket::ProxyProtocolError:
    {
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12018, robotId);
        break;
    }
    case QAbstractSocket::OperationError:
    {
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12019, robotId);
        break;
    }
    case QAbstractSocket::UnknownSocketError:
    {
        addMsg(ENUM_MSG_WARNING, "ProgramEngine", "ProgramEngine", 12020, robotId);
        break;
    }
        default:
        {
            break;
        }
    }
}

void TcpManager::newConnectionSlot()
{
    QTcpSocket *newSocket = new QTcpSocket;
    newSocket = templateServer->nextPendingConnection();
    if(!newSocket->isValid())
    {
        return;
    }
    if(tcpClientList.values().count() > this->maxPendingConnections())
    {
        return;
    }
    connect(newSocket,SIGNAL(error(QAbstractSocket::SocketError)),\
                this,SLOT(socketError(QAbstractSocket::SocketError)),Qt::DirectConnection);
    QString clientIP = newSocket->peerAddress().toString().remove(QRegExp("::ffff:"));
    qDebug() << "new Client IP:" << clientIP;

    mutex.lock();
    tcpClientList[clientIP] = newSocket;
    serverPortMap[templateServer->serverPort()] = clientIP;
    mutex.unlock();
}

void TcpManager::addMsg(int messageLevel, \
                        std::string componentName,\
                        std::string messageType,\
                        int messageCode,\
                        int robotId, \
                        int parameter1,\
                        int parameter2, \
                        int parameter3,\
                        int parameter4,\
                        QString message)
{
    Q_UNUSED(parameter1);
    Q_UNUSED(parameter2);
    Q_UNUSED(parameter3);
    Q_UNUSED(parameter4);

    Message tmpMsg;
    tmpMsg.MessageLevel = messageLevel;
    tmpMsg.componentClassName = componentName;
    tmpMsg.messageType = messageType;
    tmpMsg.robotId = robotId;
    tmpMsg.messageCode = messageCode;
   // tmpMsg.time = t1.tv_sec;

    QString infomationStr;

    switch(messageCode)
    {
        case 12030:
        {
            infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("tcpManager 读取错误!")+message;
            break;
        }
        case 12031:
        {

            infomationStr = QObject::tr("机器人:") +
               QString::number(robotId) +
               QObject::tr("tcpManager 写入错误!")+message;
            break;
        }
        case 12032:
        {
            infomationStr = QObject::tr("机器人:") +
               QString::number(robotId) +
               QObject::tr("tcpManager 未创建发送连接!")+message;
            break;
        }
    case 12033:
    {
        infomationStr = QObject::tr("机器人:") +
           QString::number(robotId) +
           QObject::tr("tcpManager 客户端写入操作错误:未创建套接字对象!")+message;
        break;
    }
    case 12034:
    {
        infomationStr = QObject::tr("机器人:") +
           QString::number(robotId) +
           QObject::tr("tcpManager 服务器端写入操作错误:未创建套接字对象!")+message;
        break;
    }
    case 12035:
    {
        infomationStr = QObject::tr("机器人:") +
           QString::number(robotId) +
           QObject::tr("tcpManager 客户端发送数据失败!")+message;
        break;
    }
    case 12036:
    {
        infomationStr = QObject::tr("机器人:") +
           QString::number(robotId) +
           QObject::tr("tcpManager 服务器端读取操作失败!")+message;
        break;
    }
    case 12037:
    {
        infomationStr = QObject::tr("机器人:") +
           QString::number(robotId) +
           QObject::tr("tcpManager 服务器端写入操作错误:未创建套接字对象!")+message;
        break;
    }
    case 12038:
    {
        infomationStr = QObject::tr("机器人:") +
           QString::number(robotId) +
           QObject::tr("tcpManager 服务器端写入操作错误:无连接对象!")+message;
        break;
    }
    case 12039:
    {
        infomationStr = QObject::tr("机器人:") +
           QString::number(robotId) +
           QObject::tr("tcpManager 客户端写入操作失败:无连接对象!")+message;
        break;
    }
    case 12040:
    {
        infomationStr = QObject::tr("机器人:") +
           QString::number(robotId) +
           QObject::tr("tcpManager 服务器端读取操作失败:无连接对象!")+message;
        break;
    }
    case 12041:
    {
        infomationStr = QObject::tr("机器人:") +
           QString::number(robotId) +
           QObject::tr("tcpManager 服务端读取操作失败:无连接对象!")+message;
        break;
    }
    case 12042:
    {
        infomationStr = QObject::tr("机器人:") +
           QString::number(robotId) +
           QObject::tr("tcpManager 服务端写入操作失败:无连接对象!")+message;
        break;
    }
    case 12000:
    {
        infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("连接服务器被拒绝或超时!")+message;
        break;
    }
    case 12001:
    {
        infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("远程主机关闭了连接!")+message;
        break;
    }
    case 12002:
    {
        infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("主机地址未找到!")+message;
        break;
    }
    case 12003:
    {
        infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("套接字操作失败，应用程序缺少必需的权限!")+message;
        break;
    }
    case 12004:
    {
        infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("本地系统耗尽资源-(例如:套接字太多)!")+message;
        break;
    }
    case 12005:
    {
        infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("套接字操作超时!")+message;
        break;
    }
    case 12006:
    {
        infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("数据报大于操作系统的限制(可以低至8192字节)!")+message;
        break;
    }
    case 12007:
    {
        infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("网络发生错误,例如:网络电缆被意外拔出!")+message;
        break;
    }
    case 12008:
    {
        infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("指定给QUdpSocket::bind()的地址已被使用,并被设置为独占!")+message;
        break;
    }
    case 12009:
    {
        infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("指定给QUdpSocket::bind()的地址不属于主机!")+message;
        break;
    }
    case 12010:
    {
        infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("本地操作系统不支持请求的套接字操作,例如:缺少IPv6支持!")+message;
        break;
    }
    case 12011:
    {
        infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("仅由QAbstractSocketEngine使用，最后一次尝试操作尚未完成,后台仍在进行中,这个值是在4.4中引入的!")+message;
        break;
    }
    case 12012:
    {
        infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("套接字使用代理，代理需要身份验证!")+message;
        break;
    }
    case 12013:
    {
        infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("SSL/TLS握手失败,因此连接已关闭,仅在QSslSocket中使用,此值在4.4中引入!")+message;
        break;
    }
    case 12014:
    {
        infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("无法联系代理服务器,因为与该服务器的连接被拒绝,此值是在4.5中引入的!")+message;
        break;
    }
    case 12015:
    {
        infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("与代理服务器的连接意外关闭,在建立与最终对等体的连接之前,此值在4.5中引入!")+message;
        break;
    }
    case 12016:
    {
        infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("与代理服务器的连接超时或代理服务器在身份验证阶段停止响应!")+message;
        break;
    }
    case 12017:
    {
        infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("未找到使用setProxy()或应用程序代理,未设置的代理地址!")+message;
        break;
    }
    case 12018:
    {
        infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("代理协议错误,无法解释来自代理服务器的响应!")+message;
        break;
    }
    case 12019:
    {
        infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("套接字操作错误!")+message;
        break;
    }
    case 12020:
    {
        infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("TcpManager 套接字未知错误!")+message;
        break;
    }

    default:
        {

        }
    }
    tmpMsg.MessageInformation = infomationStr.toStdString();
    MessageLog::getInstance()->addMessage(tmpMsg);
}
